home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / vector / LongVec.h < prev    next >
C/C++ Source or Header  |  1990-05-16  |  10KB  |  282 lines

  1. #ifndef    LONGVEC_H
  2. #define    LONGVEC_H
  3.  
  4. /* LongVec.h -- Long Integer Vectors
  5.  
  6.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  7.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  8.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  9.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  10.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  11.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  12.  
  13. Author:
  14.         Ted Persky
  15.     Bg. 12A, Rm. 2031
  16.     Computer Systems Laboratory
  17.     Division of Computer Research and Technology
  18.     National Institutes of Health
  19.     Bethesda, Maryland 20892
  20.     Phone: (301) 496-2963
  21.     uucp: uunet!nih-csl!tpersky
  22.     Internet:tpersky@alw.nih.gov
  23.  
  24. Modification History:
  25.  
  26. $Log:    LongVec.h,v $
  27.  * Revision 3.0  90/05/16  23:00:35  kgorlen
  28.  * Release for 1st edition.
  29.  * 
  30. */
  31. #include "Vector.h"
  32. #include "BitVec.h"
  33. #include "IntVec.h"
  34.  
  35. class LongSlice;
  36. class LongPick;
  37. class LongSlct;
  38.  
  39. class LongVec : public Vector {
  40.     DECLARE_MEMBERS(LongVec);
  41.     long* v;        // pointer to data, NULL if empty vector
  42.     void indexRangeErr() const;
  43. protected:
  44.     virtual void storer(OIOofd&) const;
  45.     virtual void storer(OIOout&) const;
  46. public:
  47.     LongVec(unsigned len =0);
  48.     LongVec(unsigned len, long from, long by =1);
  49.     LongVec(const long*, unsigned len);
  50.     LongVec(const LongVec&);
  51.     LongVec(const LongSlice&);
  52.     ~LongVec()            { delete v; }
  53.     LongSlice operator()(int pos, unsigned lgt, int stride =1);
  54.     const LongSlice operator()(int pos, unsigned lgt, int stride =1) const;
  55.     long* pt()        { return v; }
  56.     const long* pt() const    { return v; }
  57.     operator LongSlice();
  58.     operator const LongSlice() const;
  59.     operator DoubleVec();
  60. //    operator LongVec();
  61.     long& operator[](int i) {    // vector element
  62.         if ((unsigned)i >= n) indexRangeErr();
  63.         return v[i];
  64.     }
  65.     const long& operator[](int i) const {    // vector element
  66.         if ((unsigned)i >= n) indexRangeErr();
  67.         return v[i];
  68.     }
  69.     long& operator()(int i)            { return v[i]; }
  70.     const long& operator()(int i) const  { return v[i]; }
  71.     LongPick operator[](const IntVec&);
  72.     const LongPick operator[](const IntVec&) const;
  73.     LongSlct operator[](const BitVec&);
  74.     const LongSlct operator[](const BitVec&) const;
  75.     void /*LongVec::*/operator=(const LongVec&);
  76.     void /*LongVec::*/operator=(const LongSlice&);
  77.     void /*LongVec::*/operator=(const LongSlct&);
  78.     void /*LongVec::*/operator=(const LongPick&);
  79.     void /*LongVec::*/operator=(long);
  80.     void /*LongVec::*/lengthErr(const LongSlice&) const;
  81.     void selectErr(const BitVec&) const;
  82.     virtual void deepenShallowCopy();
  83.     virtual unsigned hash() const;
  84.     virtual bool isEqual(const Object&) const;
  85.     virtual void printOn(ostream& strm =cout) const;
  86.     virtual void scanFrom(istream& strm);
  87.     virtual void sort();
  88.     virtual const Class* species() const;
  89. };
  90.  
  91. class TempLongVec : public LongVec {
  92.     friend LongSlice;
  93.     friend LongPick;
  94.     friend LongSlct;
  95.     TempLongVec(unsigned len =0) : LongVec(len) {}
  96.     virtual void free();
  97. };
  98.  
  99. class LongSlice : public NIHCL {
  100.     LongVec* V;    // vector pointer
  101.     long* p;    // slice pointer
  102.     unsigned l;    // slice length
  103.     int k;    // slice stride
  104.     LongSlice(const LongVec& v, int pos, unsigned lgt, int stride =1);
  105.     LongSlice(const LongVec& v, unsigned lgt) {
  106.         V = &(LongVec&)v;  p = ((LongVec&)v).pt();  l = lgt;  k = 1;
  107.     }
  108.     LongSlice(const LongSlice&);
  109.     friend LongVec;
  110. public:
  111.     LongSlice(const LongPick&);
  112.     LongSlice(const LongSlct&);
  113.     ~LongSlice()        { V->free(); }
  114.     long* pt()        { return p; }
  115.     const long* pt() const    { return p; }
  116.     unsigned length() const    { return l; }
  117.     int stride() const    { return k; }
  118.     void /*LongSlice::*/operator=(const LongVec&);
  119.     void /*LongSlice::*/operator=(const LongPick&);
  120.     void /*LongSlice::*/operator=(const LongSlct&);
  121.     void /*LongSlice::*/operator=(const LongSlice&);
  122.     void /*LongSlice::*/operator=(long);
  123.     void /*LongSlice::*/lengthErr(const LongVec&) const;
  124.     void /*LongSlice::*/lengthErr(const LongSlice&) const;
  125.     void /*LongSlice::*/lengthErr(const IntVec&) const;
  126.     void selectErr(const BitVec&) const;
  127. friend    LongVec    operator-(const LongSlice&);
  128. friend    LongVec    operator!(const LongSlice&);
  129. friend    LongVec    operator~(const LongSlice&);
  130. friend    LongVec    operator++(LongSlice&);
  131. friend    LongVec    operator--(LongSlice&);
  132. friend    LongVec    operator*(const LongSlice&,const LongSlice&);
  133. friend    LongVec    operator/(const LongSlice&,const LongSlice&);
  134. friend    LongVec    operator%(const LongSlice&,const LongSlice&);
  135. friend    LongVec    operator+(const LongSlice&,const LongSlice&);
  136. friend    LongVec    operator-(const LongSlice&,const LongSlice&);
  137. friend    LongVec    operator&(const LongSlice&,const LongSlice&);
  138. friend    LongVec    operator^(const LongSlice&,const LongSlice&);
  139. friend    LongVec    operator|(const LongSlice&,const LongSlice&);
  140. friend    LongVec    operator*(const LongSlice&,long);
  141. friend    LongVec    operator*(long s,const LongSlice& V)  { return V*s; }
  142. friend    LongVec    operator/(const LongSlice&,long);
  143. friend    LongVec    operator/(long,const LongSlice&);
  144. friend    LongVec    operator%(const LongSlice&,long);
  145. friend    LongVec    operator%(long,const LongSlice&);
  146. friend    LongVec    operator+(const LongSlice&,long);
  147. friend    LongVec    operator+(long s,const LongSlice& V)  { return V+s; }
  148. friend    LongVec    operator-(const LongSlice&,long);
  149. friend    LongVec    operator-(long,const LongSlice&);
  150. friend    LongVec    operator&(const LongSlice&,long);
  151. friend    LongVec    operator&(long s,const LongSlice& V)  { return V&s; }
  152. friend    LongVec    operator^(const LongSlice&,long);
  153. friend    LongVec    operator^(long s,const LongSlice& V)  { return V^s; }
  154. friend    LongVec    operator|(const LongSlice&,long);
  155. friend    LongVec    operator|(long s,const LongSlice& V)  { return V|s; }
  156. friend    BitVec    operator<(const LongSlice&,const LongSlice&);
  157. friend    BitVec    operator>(const LongSlice& U,const LongSlice& V)    { return V < U; }
  158. friend    BitVec    operator<=(const LongSlice&,const LongSlice&);
  159. friend    BitVec    operator>=(const LongSlice& U,const LongSlice& V) { return V <= U; }
  160. friend    BitVec    operator==(const LongSlice&,const LongSlice&);
  161. friend    BitVec    operator!=(const LongSlice&,const LongSlice& V);
  162. friend    BitVec    operator<(const LongSlice&,long);
  163. friend    BitVec    operator<(long s,const LongSlice& V)  { return V > s; }
  164. friend    BitVec    operator>(const LongSlice&,long);
  165. friend    BitVec    operator>(long s,const LongSlice& V)  { return V < s; }
  166. friend    BitVec    operator<=(const LongSlice&,long);
  167. friend    BitVec    operator<=(long s,const LongSlice& V) { return V >= s; }
  168. friend    BitVec    operator>=(const LongSlice&,long);
  169. friend    BitVec    operator>=(long s,const LongSlice& V) { return V <= s; }
  170. friend    BitVec    operator==(const LongSlice&,long);
  171. friend    BitVec    operator==(long s,const LongSlice& V) { return V == s; }
  172. friend    BitVec    operator!=(const LongSlice&,long);
  173. friend    BitVec    operator!=(long s,const LongSlice& V) { return V != s; }
  174. friend    void    operator+=(LongSlice&,const LongSlice&);
  175. friend    void    operator+=(LongSlice&,long);
  176. friend    void    operator-=(LongSlice&,const LongSlice&);
  177. friend    void    operator-=(LongSlice&,long);
  178. friend    void    operator*=(LongSlice&,const LongSlice&);
  179. friend    void    operator*=(LongSlice&,long);
  180. friend    void    operator/=(LongSlice&,const LongSlice&);
  181. friend    void    operator/=(LongSlice&,long);
  182. friend    void    operator%=(LongSlice&,const LongSlice&);
  183. friend    void    operator%=(LongSlice&,long);
  184. friend    void    operator&=(LongSlice&,const LongSlice&);
  185. friend    void    operator&=(LongSlice&,long);
  186. friend    void    operator^=(LongSlice&,const LongSlice&);
  187. friend    void    operator^=(LongSlice&,long);
  188. friend    void    operator|=(LongSlice&,const LongSlice&);
  189. friend    void    operator|=(LongSlice&,long);
  190. friend    LongVec    abs(const LongSlice& V);
  191. friend    LongVec    cumsum(const LongSlice&);
  192. friend    LongVec    delta(const LongSlice&);
  193. friend    long    dot(const LongSlice&,const LongSlice&);
  194. friend    int    max(const LongSlice&);
  195. friend    int    min(const LongSlice&);
  196. friend    long    prod(const LongSlice&);
  197. friend    LongVec    reverse(const LongSlice&);
  198. friend    long    sum(const LongSlice&);
  199. };
  200.  
  201. class LongPick : public NIHCL {
  202.     LongVec* V;
  203.     const IntVec* X;
  204.     LongPick(const LongVec& v,const IntVec& x)    { V = &(LongVec&)v;  X = &x; }
  205.     LongPick(const LongPick& s)            { V = s.V; X = s.X; }
  206.     friend LongVec;
  207.     friend LongSlice;
  208.     friend LongSlct;
  209. public:
  210.     void /*LongPick::*/operator=(const LongVec&);
  211.     void /*LongPick::*/operator=(const LongPick&);
  212.     void /*LongPick::*/operator=(const LongSlct&);
  213.     void /*LongPick::*/operator=(const LongSlice&);
  214.     void /*LongPick::*/operator=(long);
  215.     unsigned length() const    { return X->length(); }
  216. };
  217.  
  218. class LongSlct : public NIHCL {
  219.     LongVec* V;
  220.     const BitVec* B;
  221.     LongSlct(const LongVec& v, const BitVec& b)    { V = &(LongVec&)v;  B = &b; }
  222.     LongSlct(const LongSlct& s)            { V = s.V; B = s.B; }
  223.     friend LongVec;
  224.     friend LongSlice;
  225.     friend LongPick;
  226. public:
  227.     void /*LongSlct::*/operator=(const LongVec&);
  228.     void /*LongSlct::*/operator=(const LongPick&);
  229.     void /*LongSlct::*/operator=(const LongSlct&);
  230.     void /*LongSlct::*/operator=(const LongSlice&);
  231.     void /*LongSlct::*/operator=(long);
  232.     unsigned length() const    { return B->length(); }
  233. };
  234.  
  235. inline LongSlice LongVec::operator()(int pos, unsigned lgt, int stride)
  236. {
  237.     LongSlice s(*this,pos,lgt,stride);
  238.     return s;
  239. }
  240.  
  241. inline const LongSlice LongVec::operator()(int pos, unsigned lgt, int stride) const
  242. {
  243.     const LongSlice s(*this,pos,lgt,stride);
  244.     return s;
  245. }
  246.  
  247. inline LongVec::operator LongSlice()
  248. {
  249.     LongSlice s(*this,length());
  250.     return s;
  251. }
  252.  
  253. inline LongVec::operator const LongSlice() const
  254. {
  255.     const LongSlice s(*this,length());
  256.     return s;
  257. }
  258.  
  259. inline LongPick LongVec::operator[](const IntVec& I)
  260. {
  261.     return LongPick(*this,I);
  262. }
  263.  
  264. inline const LongPick LongVec::operator[](const IntVec& I) const
  265. {
  266.     const LongPick t(*this,I);
  267.     return t;
  268. }
  269.  
  270. inline LongSlct LongVec::operator[](const BitVec& B)
  271. {
  272.     return LongSlct(*this,B);
  273. }
  274.  
  275. inline const LongSlct LongVec::operator[](const BitVec& B) const
  276. {
  277.     const LongSlct t(*this,B);
  278.     return t;
  279. }
  280.  
  281. #endif
  282.